package com.expense.util;

import java.util.ArrayList;
import java.util.*;
import java.util.regex.Pattern;


public class StringUtils {

    private static final String COMMA = ",";

    private static final String REG_IP = "((\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])\\.){4}";
  
    public static int parseInt(String str, int defaultVal) {
        int rs = defaultVal;
        try {
            rs = Integer.parseInt(str);
        } catch (NumberFormatException e) {
            return defaultVal;
        }
        return rs;
    }

    public static int parseInt(String str) {
        return parseInt(str, 0);
    }

    public static String chgNull(String in) {
        String out = null;
        out = in;
        if (out == null || (out.trim()).equals("null")) {
            return "";
        } else {
            return out.trim();
        }
    }

 
    public static int getPages(int size, int pageSize) {
        if (pageSize < 1)
            return 0;
        int pages = (size % pageSize == 0) ? (size / pageSize)
                : ((size / pageSize) + 1);
        return pages;
    }

  
    public static String ArrToStr(String[] arr) {
        return ArrToStr(arr, COMMA);
    }

    public static String ArrToStr(String[] arr, String comma) {
        if (arr == null)
            return "";
        String str = "";
        int size = arr.length;
        for (int i = 0; i < size; i++) {
            if (!chgNull(arr[i]).equals("")) {
                str += str.equals("") ? arr[i] : comma + arr[i];
            }
        }
        return str;
    }

    public static String ListToStr(ArrayList list, String comma) {
        String str = "";
        if (list == null || list.size() < 1) {
            return "";
        }
        int size = list.size();
        for (int i = 0; i < size; i++) {
            str += str.equals("") ? (String) list.get(i) : comma
                    + (String) list.get(i);
        }
        return str;
    }

    public static String ListToStr(ArrayList list) {
        return ListToStr(list, COMMA);
    }

    public static String[] StrToArr(String str) {
        return StrToArr(str, COMMA);
    }

   
    public static String[] StrToArr(String str, String comma) {
        String rs = chgNull(str);
        if (rs.equals(""))
            return null;
        return str.split(comma);
    }

   
    public static String HTMLEncode(String str) {
        if (str == null)
            return "";
        str.replaceAll(" ", "&nbsp;").replaceAll("\n", "<br>");
        return str;
    }

   
    public static int length(String str) {
        if (str == null)
            return 0;
        int sum = 0;
        for (int i = 0; i < str.length(); i++) {
            int ch_code = (int) str.charAt(i);
            if (ch_code < 255) {
                sum++;
            } else {
                sum += 2;
            }
        }
        return sum;
    }

   
    public static String replace(String src, String oldStr, String newStr) {
        int index = src.indexOf(oldStr);
        if (index == -1)
            return src;
        int len = src.length();
        int sublen = oldStr.length();
        return (src.substring(0, index) + newStr + replace(src.substring(index
                + sublen, len), oldStr, newStr));
    }

   
    public static String replaceQuoted(String str) {
        if (str == null)
            return "";
        if (str.indexOf("'") != -1) {
            str = replace(str, "'", "''");
        }
        return str;
    }

    public static String coding(String in) {
    	
    	String appServerTomcat = System.getProperty("Tomcat");
    	
    	if(appServerTomcat != null){
    	
        String out = null;
        out = StringUtils.chgNull(in);
        try {
            out = new String(out.getBytes("iso8859_1"), "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return out;
    	}else{
    		return in;
    	}
    }


    public static String addFix(String str, String prefix, String postfix) {
        if (str == null)
            return null;
        StringBuffer sb = new StringBuffer();
        if (prefix != null) {
            sb.append(prefix);
            sb.append(str);
        }
        if (postfix != null) {
            sb.append(postfix);
        }
        return sb.toString();
    }

    public static boolean validIP(String ip) {
        String tempIp = chgNull(ip);
        if (tempIp.equals("")) {
            return false;
        }
        Pattern pattern = Pattern.compile(REG_IP);
        return pattern.matcher(tempIp + ".").find();
    }

    public static StringBuffer getPollTitle(String netDeviceIp,
                                            String snmpString, String groupName) {
        StringBuffer sbContent = new StringBuffer("");
        sbContent.append("[IP:" + netDeviceIp + ",");
        sbContent.append("CommunityName:" + snmpString + ",");
        sbContent.append("RegExpression:1.3.6.1(.[0-9]+)*,");
        sbContent.append("GROUPNAME:" + groupName + "]\n");
        return sbContent;
    }

    public static String getPrePollContent(String netDeviceIp,
                                           String snmpString, String groupName, String descOid) {
        StringBuffer sbContent = getPollTitle(netDeviceIp, snmpString,
                groupName);
        sbContent.append("log_table\n");
        sbContent.append("{\n");
        sbContent.append(descOid + "\n");
        sbContent.append("}\n");
        return sbContent.toString();
    }

    public static int compareToday(Date startDate, Date endDate) {
        int time = 24 * 60 * 60 * 1000;
        // Date today = Formater.strToDate(Formater.dateToStr());
        // int diff = (int) ((today.getTime() - date.getTime()) / time);
        int diff = (int) ((endDate.getTime() - startDate.getTime()) / time);
        return diff;
    }

    public static int getWeekInt(Date date) {
        if (date == null)
            return -1;
        Calendar cetemp = Calendar.getInstance();
        cetemp.setTime(date);
        return cetemp.get(Calendar.DAY_OF_WEEK) - 1;
    }


    public static Date getSecondDate(Date date, int n) {
        if (date == null)
            return null;
        Calendar cetemp = Calendar.getInstance();
        cetemp.setTime(date);
        cetemp.add(Calendar.DATE, n);
        return cetemp.getTime();
    }


    public static int getMonthInt(Date date) {
        if (date == null)
            return -1;
        Calendar cetemp = Calendar.getInstance();
        cetemp.setTime(date);
        return cetemp.get(Calendar.MONTH) + 1;
    }


    public static int getDayInt(Date date) {
        if (date == null)
            return -1;
        Calendar cetemp = Calendar.getInstance();
        cetemp.setTime(date);
        return cetemp.get(Calendar.DAY_OF_MONTH);
    }

    public static Date getFirstWeekDate(Date date) {
        if (date == null)
            return null;
        Calendar cetemp = Calendar.getInstance();
        cetemp.setTime(date);
        cetemp.add(Calendar.DATE, 1 - cetemp.get(Calendar.DAY_OF_WEEK));
        return cetemp.getTime();
    }


    public static Date getLastWeekDate(Date date) {
        if (date == null)
            return null;
        Calendar cetemp = Calendar.getInstance();
        cetemp.setTime(date);
        cetemp.add(Calendar.DATE, 7 - cetemp.get(Calendar.DAY_OF_WEEK));
        return cetemp.getTime();
    }

  

    public static Date getFirstMonthDate(Date date) {
        if (date == null)
            return null;
        Calendar cetemp = Calendar.getInstance();
        cetemp.setTime(date);
        cetemp.add(Calendar.DATE, 1 - cetemp.get(Calendar.DAY_OF_MONTH));
        return cetemp.getTime();
    }

   

    public static Date getLastMonthDate(Date date) {
        if (date == null)
            return null;
        Calendar cetemp = Calendar.getInstance();
        cetemp.setTime(date);
        cetemp.set(Calendar.MONTH, cetemp.get(Calendar.MONTH) + 1);
        cetemp.add(Calendar.DATE, -cetemp.get(Calendar.DAY_OF_MONTH));
        return cetemp.getTime();
    }


    public static Date getFirstQuarterDate(Date date) {
        if (date == null)
            return null;
        Calendar cetemp = Calendar.getInstance();
        cetemp.setTime(date);
        cetemp.set(Calendar.MONTH, cetemp.get(Calendar.MONTH) - (cetemp.get(Calendar.MONTH)) % 3);
        cetemp.add(Calendar.DATE, 1 - cetemp.get(Calendar.DAY_OF_MONTH));
        return cetemp.getTime();
    }


    public static Date getLastQuarterDate(Date date) {
        if (date == null)
            return null;
        Calendar cetemp = Calendar.getInstance();
        cetemp.setTime(date);
        cetemp.set(Calendar.MONTH, cetemp.get(Calendar.MONTH) - (cetemp.get(Calendar.MONTH)) % 3 + 3);
        cetemp.add(Calendar.DATE, -cetemp.get(Calendar.DAY_OF_MONTH));
        return cetemp.getTime();
    }

   

    public static Date getFirstYearDate(Date date) {
        if (date == null)
            return null;
        Calendar cetemp = Calendar.getInstance();
		cetemp.setTime(date);
		cetemp.set(Calendar.MONTH,0);
		cetemp.add(Calendar.DATE,1 - cetemp.get(Calendar.DAY_OF_MONTH));
		return cetemp.getTime();
	}
	
	

	public static Date getLastYearDate(Date date) {
		if (date == null)
			return null;
		Calendar cetemp = Calendar.getInstance();
		cetemp.setTime(date);
		cetemp.set(Calendar.MONTH,12);
		cetemp.add(Calendar.DATE, - cetemp.get(Calendar.DAY_OF_MONTH));
		return cetemp.getTime();
	}
}

